using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace OA.BLL
{
    public class Dictionary:Base
    {
        #region 基本方法
        private DAL.Dictionary dal = null;

        public Dictionary()
        {
            isession = DB_help.NHibernateHelper.GetCurrentSession(DefaultDataBase);
            dal = new DAL.Dictionary(isession);
        }

        public Common.Result Add(Model.Dictionary value)
        {
            Common.Result ret = new Common.Result(false, 0, "添加失败");
            if (value.Type == Common.EnumType.DictionaryType.Dictionary)
            {
                if (dal.GetDictionary(value.Code) != null)
                {
                    ret.message = "[编号]已经存在";
                    return ret;
                }
            }
            else
            {
                if (value.ParentID == null)
                {
                    ret.message = "数据类型的字典必须选择[上级字典]";
                    return ret;
                }
                if (dal.GetData(value.ParentID.Value, value.Code) != null)
                {
                    ret.message = "[编号]已经存在";
                    return ret;
                }
                
            }
            if (value.ParentID != null)
            {
                value.Parent = dal.GetModel(value.ParentID);
                if (value.Parent == null)
                {
                    ret.message = "[上级字典]不存在";
                    return ret;
                }
                if (value.Parent.Type == Common.EnumType.DictionaryType.Value)
                {
                    ret.message = "无法将数据类型字典作为[上级字典]";
                    return ret;
                }
            }
            ret = dal.Add(value);
            return ret;
        }

        public Common.Result Update(Model.Dictionary value)
        {
            Common.Result ret = new Common.Result(false, 0, "修改失败");
            var dalvalue = dal.GetModel(value.ID);
            if (dalvalue == null)
            {
                ret.message = "数据不存在,无法修改";
                return ret;
            }
            if (value.ParentID == null)
            {
                if (value.Type == Common.EnumType.DictionaryType.Value)
                {
                    ret.message = "数据类型的字典必须拥有上级字典";
                    return ret;
                }
            }
            else if (value.ParentID != dalvalue.ParentID)
            {
                value.Parent = dal.GetModel(value.ParentID);
                if (value.Parent == null)
                {
                    ret.message = "[上级字典]不存在";
                    return ret;
                }
                if (value.Parent.Type == Common.EnumType.DictionaryType.Value)
                {
                    ret.message = "无法将数据类型字典作为[上级字典]";
                    return ret;
                }
                if (dal.GetData(value.ParentID.Value, value.Code) != null)
                {
                    ret.message = "[编号]已经存在";
                    return ret;
                }
                var chk = value.Parent;
                while (chk != null)
                {
                    if (chk.ID == value.ID)
                    {
                        ret.message = "要修改的上级字典为当前字典的下级字典，无法修改。";
                        return ret;
                    }
                    chk = chk.Parent;
                }
            }
            dalvalue.ParentID = value.ParentID;
            dalvalue.Code = value.Code;
            dalvalue.DictionaryName = value.DictionaryName;
            ret = dal.Update(dalvalue);
            return ret;
        }

        public Common.Result Delete(string ids)
        {
            var ret=dal.Delete(ids);
            return ret;
        }

        public Model.Dictionary Get(int id)
        {
            return dal.GetModel(id);
        }

        public IList<Model.Dictionary> Get(string where, string orderby, int start, int length, params object[] ps)
        {
            return dal.Get(where, orderby, start, length, ps);
        }

        public int GetCount(string where, params object[] ps)
        {
            return dal.GetCount(where, ps);
        }
        #endregion

        public IList<Model.Dictionary> GetChilds(string Code)
        {
            return dal.GetChilds(Code);
        }
    }
}
